package cn.edu.nuc.onlinestore.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
     
public class SerilizableText {
	
    public static File f = new File("e:/store/"); 
	// 序列化
	public static <T> void Serializable(String p,String path, T t) {
        File mirdk = new File(f + p);
        if(!mirdk.exists()){              //判断该目录是否存在。（不存在自动生成）
        	mirdk.mkdirs();
        }
		File file = new File(mirdk + path );
		try (
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
		){
			oos.writeObject(t);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 反序列化
	public static <T> T deSerializable(File file) {
		try (
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		){
            @SuppressWarnings("unchecked")
			T t =  (T) ois.readObject();
            return t;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
	
	//反序列化所有产品
	public static <T> List<T> deSeralizableAll(String path){
		File f = new File(SerilizableText.f + path);
		File[] files = f.listFiles();
		List<T> list = new ArrayList<>();
		
		for(File file : files){
			T t = deSerializable(file);
			list.add(t);
		}
		return null;
	  	
	}
}
